package com.lu.common.task.service.impl;

import com.lu.common.quartz.domain.ScheduleJob;
import com.lu.common.quartz.utils.QuartzManage;
import com.lu.common.task.dao.TaskDao;
import com.lu.common.utils.ScheduleJobUtils;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.lu.common.task.domain.TaskDO;
import com.lu.common.task.service.TaskService;

@Service
public class TaskServiceImpl implements TaskService {
	@Autowired
	private TaskDao taskDao;
	@Autowired
	private QuartzManage quartzManage;
	
	@Override
	public TaskDO get(Long id){
		return taskDao.get(id);
	}
	
	@Override
	public List<TaskDO> list(Map<String, Object> map){
		return taskDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return taskDao.count(map);
	}
	
	@Override
	public int save(TaskDO task){
		return taskDao.save(task);
	}

    @Override
    public void saveBatch(List<TaskDO> task) throws Exception {
		taskDao.saveBatch(task);
    }
	
	@Override
	public int update(TaskDO task){
		return taskDao.update(task);
	}

    @Override
    public void updateBatch(List<TaskDO> task) throws Exception {
		taskDao.updateBatch(task);
    }
	
	@Override
	public int remove(Long id){
		return taskDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return taskDao.batchRemove(ids);
	}

	@Override
	public void initSchedule() throws SchedulerException {
		// 获取数据库存在的任务
		List<TaskDO> jobList = taskDao.list(null);
		for(TaskDO job : jobList){
			ScheduleJob scheduleJob = ScheduleJobUtils.entityToData(job);
			quartzManage.addJob(scheduleJob);
		}
	}

}
